# 尝试导入 requests 模块，并处理导入失败的情况
try:
    import requests
except ImportError:
    print("requests 模块未安装，请运行 'pip install requests' 进行安装")
    # 提供自动安装选项（可选）
    user_input = input("是否自动安装 requests 模块？(y/n): ").strip().lower()
    if user_input == 'y':
        import subprocess
        import sys
        subprocess.check_call([sys.executable, "-m", "pip", "install", "requests"])
        import requests
    else:
        print("模块未安装，程序将退出。")
        exit(1)  # 中止程序运行

import os

# 照片的URL地址
url = "http://n.sinaimg.cn/news/crawl/117/w550h367/20220417/a3a9-08b2ca6dffe4210149dda424adf0734f.jpg"

# 获取桌面路径
desktop_path = os.path.join(os.path.expanduser("~"), "Desktop")

# 构建保存图片的完整路径
file_path = os.path.join(desktop_path, "downloaded_image.jpg")

# 发送HTTP请求获取图片内容
try:
    response = requests.get(url, timeout=10)  # 设置超时时间
    response.raise_for_status()  # 检查请求是否成功
except requests.exceptions.Timeout:
    print("请求超时，请检查网络连接或尝试其他URL。")
except requests.exceptions.HTTPError as http_err:
    print(f"HTTP 错误发生: {http_err}")
except requests.exceptions.ConnectionError:
    print("连接错误，请检查网络设置或URL地址。")
except requests.exceptions.RequestException as e:
    print(f"下载图片失败: {e}")
else:
    # 将图片内容写入文件
    with open(file_path, 'wb') as file:
        file.write(response.content)
    print(f"图片已成功保存到 {file_path}")